﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Reflection;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace HyperCrunch.Druid
{
    [Serializable()]
    public class Rule
    {
        public static string GetAttributeForProperty(string propertyName)
        {
            switch (propertyName)
            {
                case "Id": return Globals.ID_ATTRIBUTE;
                case "IgnoreIf": return Globals.IGNORE_IF_ATTRIBUTE;
                case "AllowOverride": return Globals.ALLOW_OVERRIDE_ATTRIBUTE;
                case "Order": return Globals.ORDER_ATTRIBUTE;
                case "ContinueAfterMatch": return Globals.CONTINUE_AFTER_MATCH_ATTRIBUTE;

                case "MatchType": return Globals.MATCH_TYPE_ATTRIBUTE;
                case "MatchField": return Globals.MATCH_FIELD_ATTRIBUTE;
                case "MatchValues": return Globals.MATCH_VALUES_ATTRIBUTE;
                case "MatchCondition": return Globals.MATCH_CONDITION_ATTRIBUTE;

                case "SkinControl": return Globals.DATA_SKIN_CONTROL_ATTRIBUTE;
                case "ContainerControl": return Globals.DATA_CONTAINER_CONTROL_ATTRIBUTE;
                case "Theme": return Globals.DATA_THEME_ATTRIBUTE;
                case "ContentRenderer": return Globals.DATA_CONTENT_RENDERER_ATTRIBUTE;
                case "ContentModifier": return Globals.DATA_CONTENT_MODIFIER_ATTRIBUTE;
                case "StylesheetUrl": return Globals.DATA_STYLESHEET_URL_ATTRIBUTE;
                case "ScriptUrl": return Globals.DATA_SCRIPT_URL_ATTRIBUTE;
                case "RedirectUrl": return Globals.DATA_REDIRECT_URL_ATTRIBUTE;
                case "JsonSettingName": return Globals.JSON_SETTING_NAME_ATTRIBUTE;
                case "JsonSettingValue": return Globals.JSON_SETTING_VALUE_ATTRIBUTE;
                case "Debug": return Globals.DEBUG_ATTRIBUTE;
            }

            return String.Empty;
        }

        [XmlAttribute(AttributeName = Globals.ID_ATTRIBUTE)]
        public string Id { get; set; }

        // Needed for Rule Editor UI to enable drag-and-drop re-ordering of rules
        public string ParentId { get; set; }

        [XmlAttribute(AttributeName = Globals.DEBUG_ATTRIBUTE)]
        public string Debug { get; set; }

        [XmlAttribute(AttributeName = Globals.MATCH_TYPE_ATTRIBUTE)]
        public string MatchType { get; set; }

        [XmlAttribute(AttributeName = Globals.MATCH_FIELD_ATTRIBUTE)]
        public string MatchField { get; set; }

        [XmlAttribute(AttributeName = Globals.MATCH_VALUES_ATTRIBUTE)]
        public string MatchValues { get; set; }

        private MatchConditionType matchCondition = MatchConditionType.Equal;
        [XmlAttribute(AttributeName = Globals.MATCH_CONDITION_ATTRIBUTE)]
        public MatchConditionType MatchCondition
        {
            get { return matchCondition; }
            set { matchCondition = value; }
        }

        private IgnoreConditionType ignoreIf = IgnoreConditionType.Never;
        [XmlAttribute(AttributeName = Globals.IGNORE_IF_ATTRIBUTE)]
        public IgnoreConditionType IgnoreIf
        {
            get { return ignoreIf; }
            set { ignoreIf = value; }
        }

        private AllowOverrideType allowOverride = AllowOverrideType.Never;
        [XmlAttribute(AttributeName = Globals.ALLOW_OVERRIDE_ATTRIBUTE)]        
        public AllowOverrideType AllowOverride
        {
            get { return allowOverride; }
            set { allowOverride = value; }
        }

        private int order = 1000000;
        [XmlAttribute(AttributeName = Globals.ORDER_ATTRIBUTE)]
        public int Order
        {
            get { return order; }
            set { order = value; }
        }

        private ContinueAfterMatchType continueAfterMatch = ContinueAfterMatchType.Never;
        [XmlAttribute(AttributeName = Globals.CONTINUE_AFTER_MATCH_ATTRIBUTE)]
        public ContinueAfterMatchType ContinueAfterMatch
        {
            get { return continueAfterMatch; }
            set { continueAfterMatch = value; }
        }

        public bool IsHostRule { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_SKIN_CONTROL_ATTRIBUTE)]
        public string SkinControl { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_CONTAINER_CONTROL_ATTRIBUTE)]
        public string ContainerControl { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_THEME_ATTRIBUTE)]
        public string Theme { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_CONTENT_RENDERER_ATTRIBUTE)]
        public string ContentRenderer { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_CONTENT_MODIFIER_ATTRIBUTE)]
        public string ContentModifier { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_STYLESHEET_URL_ATTRIBUTE)]
        public string StylesheetUrl { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_SCRIPT_URL_ATTRIBUTE)]
        public string ScriptUrl { get; set; }

        [XmlAttribute(AttributeName = Globals.DATA_REDIRECT_URL_ATTRIBUTE)]
        public string RedirectUrl { get; set; }

        [XmlAttribute(AttributeName = Globals.JSON_SETTING_NAME_ATTRIBUTE)]
        public string JsonSettingName { get; set; }

        [XmlAttribute(AttributeName = Globals.JSON_SETTING_VALUE_ATTRIBUTE)]
        public string JsonSettingValue { get; set; }

        public RuleCollection Rules { get; set; }

    }
}

